//
//  TSSaveSuccessTool.swift
//  Pods
//
//  Created by 100Years on 2025/4/24.
//

public let kSaveSuccesswShared = TSSaveSuccessTool.shared
open class TSSaveSuccessTool {
    
    static let shared = TSSaveSuccessTool()
    
    public var clickViewHandle:(()->Void)?
    
    public lazy var textLabel:UILabel = {
        let textLabel = UILabel()
        textLabel.textColor = UIColor.white
        textLabel.text = "Save Successfully".localized
        textLabel.font = UIFont.font(size: 14)
        return textLabel
    }()
    
    public lazy var saveSuccessBg: UIView = {
        return creatSaveSuccessBg()
    }()
    
    
    public lazy var viewButton:UIButton = {
        let color = "4FEA9D".uiColor
        let viewButton = UIButton.createButton(title: "View".localized ,backgroundColor: color.withAlphaComponent(0.1),font: UIFont.font(size: 12),titleColor: color,corner: 14) { [weak self]  in
            guard let self = self else { return }
            if let clickViewHandle = clickViewHandle {
                clickViewHandle()
            }else {
                if let url = URL(string: "photos-redirect://") {
                    if UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    }
                }
            }
            
            DispatchQueue.main.async{
                self.saveSuccessBg.removeFromSuperview()
            }
        }
        viewButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        return viewButton
    }()
    
    
    
    public lazy var colorBg: UIView = {
        let colorBg = UIView()
        colorBg.backgroundColor = "#333333".uiColor
        colorBg.layer.cornerRadius = 8
        colorBg.layer.masksToBounds = true
        colorBg.clipsToBounds = true
        return colorBg
    }()
    
    
    public lazy var iconView: UIImageView = {
        let image = UIImage(named: "success_icon")
        let iconView = UIImageView(image: image)
        return iconView
    }()
    
    func creatSaveSuccessBg() -> UIView {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 288, height: 48)
        // 阴影
        view.backgroundColor = .clear
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOffset = CGSize(width: 0, height: 2)
        view.layer.shadowOpacity = 0.1
        
        view.addSubview(colorBg)
        colorBg.snp.makeConstraints { make in
            make.leading.trailing.top.bottom.equalTo(0)
        }
        
        view.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalTo(12)
        }
    
        view.addSubview(viewButton)
        view.addSubview(textLabel)
        viewButton.snp.makeConstraints { make in
            make.width.equalTo(viewButton.intrinsicContentSize.width)
            make.height.equalTo(28)
            make.trailing.equalTo(-8)
            make.centerY.equalToSuperview()
        }
    
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(8)
            make.trailing.equalTo(viewButton.snp.leading).offset(-8)
            make.centerY.equalToSuperview()
        }

        return view
    }

    
    
    public func getBottom(topY:CGFloat)->CGFloat{
        let bottom = -(k_ScreenHeight - 48 - topY)
        debugPrint("bottom=\(bottom)")
        return bottom
    }
    
    public func show(atView:UIView,text:String = "Save Successfully".localized,deadline:Double = 5.0,bottom:CGFloat = -112,showViewBtn:Bool = true,clickViewHandle:(()->Void)? = nil) {
        self.clickViewHandle = clickViewHandle
        kExecuteOnMainThread {
            self.textLabel.text = text
            self.viewButton.isHidden = !showViewBtn
            atView.addSubview(self.saveSuccessBg)
            self.saveSuccessBg.snp.remakeConstraints { make in
                make.width.greaterThanOrEqualTo(288)
                make.width.lessThanOrEqualTo(k_ScreenWidth-32)
                make.height.equalTo(48)
                make.centerX.equalToSuperview()
                make.top.equalTo(k_Nav_Height + 10)
            }
            
            
            self.viewButton.setTitle("View".localized, for: .normal)
            self.viewButton.snp.remakeConstraints { make in
                make.width.equalTo(self.viewButton.intrinsicContentSize.width)
                make.height.equalTo(28)
                make.trailing.equalTo(-8)
                make.centerY.equalToSuperview()
            }
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + deadline) {
            self.saveSuccessBg.removeFromSuperview()
        }
    }
    
    
}
